package mvp.levelup.spring.detectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * Created by Valera on 25.08.2014.
 */
@Component("keeper")
public class HouseKeeper {

    private IDetector fireDetector;
    @Resource(name = "doorDetector")
    private IDetector myDetector;
    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    public HouseKeeper(IDetector fireDetector) {
        this.fireDetector = fireDetector;
        String fireDetectorState = fireDetector != null ? "Fire Detector is OK" : "Fire Detector is NULL";
        System.err.println("Constructor: " + fireDetectorState);
        String doorDetectorState = myDetector != null ? "Door Detector is OK" : "Door Detector is NULL";
        System.err.println("Constructor: " + doorDetectorState);
    }

    @PostConstruct
    public void postConstruct() {
        String fireDetectorState = fireDetector != null ? "Fire Detector is OK" : "Fire Detector is NULL";
        System.err.println("PostConstruct: " + fireDetectorState);
        String doorDetectorState = myDetector != null ? "Door Detector is OK" : "Door Detector is NULL";
        System.err.println("PostConstruct: " + doorDetectorState);
        String appContextState = applicationContext != null ? "App Context is OK" : "App Context is NULL";
        System.err.println("PostConstruct: " + appContextState);
    }

    @PreDestroy
    public void destroy(){
        System.err.println("I am dead");
    }
}
